package com.example.bloguser.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.xpd.blog.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class CodeController {
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/code")
    public Result createCaptcha(HttpServletResponse response) throws IOException {
        int width = 200;
        int height = 45;
        int numCode = 4;
        int numLine = 50;
        LineCaptcha line = CaptchaUtil.createLineCaptcha(width, height, numCode, numLine);
        String str = line.getCode();
        System.out.println(str);
       // req.getSession().setAttribute("code", str);
        String imageb = line.getImageBase64Data();
        redisTemplate.opsForValue().set("loginCode",line.getCode());

        System.out.println("loginCode = " + redisTemplate.opsForValue().get("loginCode"));
        return new  Result(1,"",imageb);
    }


}
